// Copyright(c) 2008 Tri Tech Information Systems Inc. 
// Distributed under the Boost Software License, Version 1.0.
//     (See accompanying file ../../LICENSE_1_0.txt or copy at
//           http://www.boost.org/LICENSE_1_0.txt)
//     
#include <boost/python.hpp>
#include <vector>


template<typename HeldType>
struct expose_vector
{
    typedef expose_vector<HeldType> register_type;
    typedef std::vector<HeldType> vector_type;
    expose_vector()
    {
        using namespace boost::python;

        converter::registry::push_back(
            register_type::convertible,
            register_type::construct,
            type_id< vector_type >() );
    }

    static void * convertible(PyObject * py_obj)
    {
        // we are supposed to indicate whether or not we can convert this
        // we don't really know, but we'll try any sequence
        if( PySequence_Check(py_obj) )
            return py_obj;
        return 0;
    }

    static void construct( PyObject * py_obj, boost::python::converter::rvalue_from_python_stage1_data* data)
    {
        using namespace boost::python;
        typedef converter::rvalue_from_python_storage< vector_type > storage_t;
        
        storage_t* the_storage = reinterpret_cast<storage_t*>( data );
        void* memory_chunk = the_storage->storage.bytes;
        vector_type * newvec = new (memory_chunk) vector_type;
        data->convertible = memory_chunk;

        object sequence(handle<>( borrowed( py_obj ) ) );

        for(int idx = 0; idx < len(sequence);idx++)
        {
            newvec->push_back( extract<HeldType>( sequence[idx] )() );
        }

    }
};
